package parts;

import java.util.ArrayList;

public class CompositePart extends APart {
	
	private String name;
	private double value;
	private ArrayList<APart> parts;
	
	/**
	 * @param partName The name of the part
	 * @param value TODO
	 * @param partValue The value of the part
	 */
	public CompositePart(String partName, double partValue) {
		parts = new ArrayList<APart>();
		name = partName;
		value = partValue;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public double getValue() {
		double sum = 0.0;
		
		if (!parts.isEmpty()) {
			for (APart part : parts) {
				sum += part.getValue();
			}
		} else {
			sum = value;
		}
				
		return sum;
	}

	@Override
	public void setName(String newName) {
		name = newName;
	}

	@Override
	public void setValue(double newValue) {
		System.out.println("Can only set the value of sub parts");
	}
	
	public void addPart(APart aPart) {
		parts.add(aPart);
	}
	
	public APart removePart(int index) {
		try {
			return parts.remove(index);
		} catch (IndexOutOfBoundsException e) {
			System.err.println("Part already removed");
			return null;
		}
	}
	
	public boolean isComposite() {
		return true;
	}
	
	public String toString() {
		return name;
	}

}
